//
//  LLUserAccountTools.swift
//  xinlangweibo
//
//  Created by 任玉林 on 16/6/26.
//  Copyright © 2016年 任玉林. All rights reserved.
//

import UIKit

class LLUserAccountTools: NSObject {
    
    //路径
    let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.archiver")
    
    //要保存的对象
    var userAccount: LLUserAccount?
    
    //判断用户是否登录
    var isLogin: Bool {
        
        return accountToken != nil
    }
    
    //访问令牌
    var accountToken: String? {
        
        if userAccount?.access_token == nil {
            
            return nil
        }else{
            
            if userAccount?.expires_Date?.compare(NSDate()) == NSComparisonResult.OrderedDescending{
                
                return userAccount?.access_token
            }else{
                
                return nil
            }
            
        }
        
    }
    
    override init() {
        
        super.init()
        
        userAccount = getUserAccount()
    }
    
    //单例
    static let sharedTools:LLUserAccountTools = LLUserAccountTools()
    
    
    //保存
    func saveUserAccount(userAccount:LLUserAccount){
        
        self.userAccount = userAccount
        
        //归档
        NSKeyedArchiver.archiveRootObject(userAccount, toFile: path)
        
    }
    
    //解档
    func getUserAccount()->LLUserAccount?{
        
        let resutl = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as?LLUserAccount
        
        return resutl
    }
    
    

}
